<?php

class Role_model extends MY_CI_Model
{

    /**
     * @access public
     */
    public function __construct()
    {
        $this->table        = 'role';
        $this->primary_id   = 'id';
        $this->primary_name = 'role_name';

        parent::__construct();
    }

    /**
     * 统计数量
     * @param array $filter
     * @return number
     */
    public function get_list_count($filter = array())
    {
        $this->db->from($this->table . ' as a');
        $this->list_query_filter($filter);
        return (int)$this->db->count_all_results();
    }

    /**
     * 获取分页数据
     * @param array $filter
     * @param number $page
     * @param number $page_size
     * @return multitype:Ambigous <multitype:, number> multitype:string
     */
    public function get_list($filter = array(), $page = 1, $page_size = 15)
    {
        $arr = array();
        //初始化条件
        if (isset($filter['sort']) && !empty($filter['sort'])) {
            $filter['sort'] = trim($filter['sort']);
            if (!$this->check_single_field($filter['sort'])) {
                $filter['sort'] = 'a.add_time';
            }
        } else {
            $filter['sort'] = 'a.add_time';
        }
        $filter['order'] = isset($filter['order']) && !empty($filter['order']) ? trim($filter['order']) : 'desc';
        //获取记录数
        $record_count = $this->get_list_count($filter);
        $pager        = $this->page_and_size($filter, $record_count, $page, $page_size);

        if ($pager['record_count'] > 0) {
            $this->db->select('a.*');
            $this->db->from($this->table . ' as a');
            $this->list_query_filter($filter);
            $this->query_list_sort_order($filter);
            $this->db->limit($pager['page_size'], $pager['start']);
            $item_list = $this->db->get()->result_array();
            $arr       = $item_list;
        }
        return array('list' => $arr, 'pager' => $pager);
    }

    /**
     * 新增数据
     * @param unknown $datas
     */
    public function add_data($data_results)
    {
        $data_results['add_time']    = time();
        $db_data                     = $this->check_post_form_datas($data_results);
        if (!empty($db_data)) {
            $this->db->insert($this->table, $db_data);
            $data_results['id']              = $this->db->insert_id();
            $this->result[$this->primary_id] = $data_results['id'];
        } else {
            $this->result['error']   = 1;
            $this->result['message'] = '未写入任何数据';
        }
        return $this->result;
    }


    /**
     * 修改数据
     * @param unknown $data_results
     * @param unknown $primary_id
     * @return multitype:number string
     */
    public function edit_data($data_results, $primary_id, $where_in = FALSE)
    {
        try {
            $db_data = $this->check_post_form_datas($data_results);
            if (!empty($db_data)) {
                $this->db->where($this->primary_id, $primary_id);
                $this->db->update($this->table, $db_data);
                $this->result[$this->primary_id] = $primary_id;
            } else {
                $this->result['error']   = 1;
                $this->result['message'] = '未更新任何数据';
            }
        } catch (exception $e) {
            $this->result['error']   = 1;
            $this->result['message'] = $e->getMessage();
        }
        return $this->result;
    }

    /**
     * 查询条件
     * @param array $filter
     * @return void
     */
    function list_query_filter($filter = array())
    {
        $this->db->where('a.is_delete', 0);
        if (isset($filter['keywords']) && !empty($filter['keywords'])) {
            $filter['keywords'] = trim($filter['keywords']);
            $whereKW            = "(a." . $this->primary_name . " like '%" . $filter['keywords'] . "%' or a." . $this->primary_id . " like '%" . $filter['keywords'] . "%')";
            $this->db->where($whereKW);
        }
    }

    /**
     * 获取角色列表
     * @return mixed
     */
    public function get_role_list()
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('active', 1);
        $this->db->where('is_delete', 0);
        return $this->db->get()->result_array();
    }


}

?>